package com.twotiger.and.activity.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import app.twotiger.p2p.R;

import com.twotiger.and.adapter.ProjectDetaillistAdpter.ProjectDetailItem;
import com.twotiger.and.adapter.ViewHolder;

/**
 * baseList通用item
 * @Description: 
 *     适用于：更多类 样式列表
 * @Author douxy
 * @Date 2015年8月21日 下午5:17:55
 */
public class BaseListItemLayout2 extends AbstractListItemLayout {

    private View header;
    private View devide_line;
    private RelativeLayout.LayoutParams devideLineParams;
    private TextView base_list_item_key;
    private TextView base_list_item_desc;
    private ImageView base_list_item_icon;
    private ImageView base_list_item_right_icon;
    private View convertView;

    public View inflate(Context context, int position, View view, AbastractListItem item) {
        return inflate(context, position, view, item, null);
    }

    @Override
    public View inflate(Context context, int position, View view, AbastractListItem item, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        if (view == null) {
            view = inflater.inflate(R.layout.base_list_item_2, null);
        }
        this.convertView = view;
        header = ViewHolder.get(convertView, R.id.header);
        devide_line = ViewHolder.get(convertView, R.id.devide_line);
        base_list_item_key = ViewHolder.get(convertView, R.id.base_list_item_key);
        base_list_item_desc = ViewHolder.get(convertView, R.id.base_list_item_desc);
        base_list_item_icon = ViewHolder.get(convertView, R.id.base_list_item_icon);
        base_list_item_right_icon = ViewHolder.get(convertView, R.id.base_list_item_right_icon);
        devideLineParams = (RelativeLayout.LayoutParams) devide_line.getLayoutParams();

        //初始化
        header.setVisibility(View.GONE);
        devide_line.setVisibility(View.VISIBLE);
        devideLineParams.leftMargin = context.getResources().getDimensionPixelSize(R.dimen.dp_70);
        base_list_item_icon.setVisibility(View.GONE);
        base_list_item_key.setVisibility(View.INVISIBLE);
        base_list_item_desc.setVisibility(View.GONE);
        base_list_item_key.setTextColor(context.getResources().getColor(R.color.gray_2));
        base_list_item_desc.setTextColor(context.getResources().getColor(R.color.gray_1));
        base_list_item_right_icon.setVisibility(View.VISIBLE);
        getView().setOnClickListener(null);
        fillData((ProjectDetailItem) item, position);
        return getView();
    }

    public void first() {
        header.setVisibility(View.GONE);
        devide_line.setVisibility(View.GONE);
        devideLineParams.leftMargin = 0;
    }

    public void setDevideLineLeft(int leftMarginPx) {
        devideLineParams.leftMargin = leftMarginPx;
    }

    public void hideRightIcon() {
        base_list_item_right_icon.setVisibility(View.INVISIBLE);
    }

    public void setKey(String key) {
        base_list_item_key.setText(key);
        base_list_item_key.setVisibility(View.VISIBLE);
    }

    public void setDesc(String desc) {
        base_list_item_desc.setText(desc);
        base_list_item_desc.setVisibility(View.VISIBLE);
    }

    public void fillData(final ProjectDetailItem baseListItem, int position) {

        //第一条
        if (position == 0 || baseListItem.nextBlock) {
            first();
        }
        //不可以点击
        if (baseListItem.onclick == null) {
            hideRightIcon();
        } else {
            getView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    baseListItem.onClick();
                }
            });
        }
        if (baseListItem.key != null) {
            setKey(baseListItem.key);
        }
        if (baseListItem.desc != null) {
            setDesc(baseListItem.desc);
        }
        if (baseListItem.icon != 0) {
            base_list_item_icon.setVisibility(View.VISIBLE);
            base_list_item_icon.setImageResource(baseListItem.icon);
        }
    }

    public View getView() {
        return convertView;
    }

}
